package com.tintintech.sendmail.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import com.tintintech.sendmail.Config;
import com.tintintech.sendmail.helper.DataHelper;
import com.tintintech.sendmail.helper.FileHelper;
/**
 * 统计 
 * @author frk
 */
public class MailStat {

	private static ConcurrentLinkedQueue<String> sendFailedLogQueue = new ConcurrentLinkedQueue<String>();
	
	public static long START_TIMESTAMP = 0;
	
	/**
	 * 相当于锁
	 */
	public static AtomicBoolean MAILBOX_LOAD_DATA = new AtomicBoolean(false);
	
	public static AtomicInteger SEND_SUCCESS_NUM_1 = new AtomicInteger(0);
	public static AtomicInteger SEND_SUCCESS_NUM_2 = new AtomicInteger(0);
	public static AtomicInteger SEND_FAILED_NUM_1 = new AtomicInteger(0);
	public static AtomicInteger SEND_FAILED_NUM_2 = new AtomicInteger(0);
	public static AtomicInteger BDB_STORE_NUM = new AtomicInteger(0);
	public static AtomicInteger RECV_SUCCESS_NUM = new AtomicInteger(0);
	public static AtomicInteger MAIL_DISCARD_NUM_1 = new AtomicInteger(0);
	public static AtomicInteger MAIL_DISCARD_NUM_2 = new AtomicInteger(0);
	public static AtomicInteger MAIL_SEND_NUM = new AtomicInteger(0);
	public static AtomicInteger MAIL_SEND_SUCCESS_NUM = new AtomicInteger(0);
	public static AtomicInteger DB_PUT_NUM = new AtomicInteger(0);
	public static AtomicInteger DB_GET_NUM = new AtomicInteger(0);
	public static AtomicInteger DB_DElETE_NUM = new AtomicInteger(0);
	
	public static String log = Config.GLOBALS.get("logPath") + Config.GLOBALS.get("logStatName"); 
	
	public static void sync()
	{
		System.out.println("stat sync log");
		writelog(getData(), null);
		FileHelper.recordLog(getSendFailedLogs(), Config.GLOBALS.get("logPath") + Config.GLOBALS.get("logFailName"));
	}
	
	public static String getData()
	{
		Map<String, String> statMap = new HashMap<String, String>();
		statMap.put("send_success_num_1", MailStat.SEND_SUCCESS_NUM_1.toString());
		statMap.put("send_success_num_2", MailStat.SEND_SUCCESS_NUM_2.toString());
		statMap.put("send_failed_num_1", MailStat.SEND_FAILED_NUM_1.toString());
		statMap.put("send_failed_num_2", MailStat.SEND_FAILED_NUM_2.toString());
		statMap.put("recv_success_num", MailStat.RECV_SUCCESS_NUM.toString());
		statMap.put("thread_num", new Integer(Thread.activeCount()).toString());
		statMap.put("wait_send_num", new Integer(DataHelper.MAILMAP_QUEUE.size()).toString());
		statMap.put("bdb_mail_num", MailStat.BDB_STORE_NUM.toString());
		statMap.put("mail_discard_num_1", MailStat.MAIL_DISCARD_NUM_1.toString());
		statMap.put("mail_discard_num_2", MailStat.MAIL_DISCARD_NUM_2.toString());
		statMap.put("mail_send_num", MailStat.MAIL_SEND_NUM.toString());
		statMap.put("mail_send_success_num", MailStat.MAIL_SEND_SUCCESS_NUM.toString());
		statMap.put("db_put_num", MailStat.DB_PUT_NUM.toString());
		statMap.put("db_get_num", MailStat.DB_GET_NUM.toString());
		statMap.put("db_del_num", MailStat.DB_DElETE_NUM.toString());
		
		long endTime = System.currentTimeMillis() - MailStat.START_TIMESTAMP;
		Long timestamp = endTime / 1000;
		statMap.put("uptime", new Long(timestamp).toString());
		
		return DataHelper.JSON.encode(statMap);
	}
	
	public static String getSendFailedLogs()
	{
		StringBuffer s = new StringBuffer();
		String r = null;
		while ((r = MailStat.sendFailedLogQueue.poll()) != null) {
			s.append(r + "\n");
		}
		return s.toString();
	}
	
	public static void setSendFailedItems(String e)
	{
		MailStat.sendFailedLogQueue.offer(e);
	}
	
	private static void writelog(String info, String uri)
	{
		if (uri == null)
		{
			uri = log;
		}
		File fd = new File(uri);
		if (fd.isFile() == true)
		{
			fd.delete();
		}
		try
		{
			if (fd.createNewFile())
			{
				FileWriter fw = new FileWriter(fd);
				fw.write(info);
				fw.flush();
				fw.close();
			}
		}
		catch (IOException ioe)
		{
			
		}
		catch (SecurityException se)
		{
			se.printStackTrace();
		}
	}
	
	public static String readlog()
	{
		File fd = new File(log);
		if (fd.exists() == false)
		{
			return "";
		}
		try
		{
			FileReader fr = new FileReader(log);
			BufferedReader reader = new BufferedReader(fr);
			return reader.readLine();
		}
		catch (FileNotFoundException nfe)
		{
			
		}
		catch (IOException ioe)
		{
			
		}
		
		return "";
	}
}
